/*
 * RetrieveRequestHandler.java
 *
 * Created on October 17, 2007, 10:22 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import org.adl.registry.website.rim.RimProxy;

/**
 *
 * @author marksj
 */
public abstract class RetrieveRequestHandler extends RequestHandler
{
    private String mResponseType;
    private String mContentObjectIdentifier;
    private String mMetadataInstanceIdentifier;
    
    /**
     * Creates a new instance of RetrieveRequestHandler
     */
    public RetrieveRequestHandler()
    {
    }
    
    /**
     *
     * @return
     * @param iRimUrl
     * @param iClientAccessKey
     * @param iRegistry
     * @param iQuery
     * @throws java.io.IOException
     */
    public String sendRetrieveRequest()
    throws IOException
    {
        String rimResponse;
        
        ServletContext context = super.getServletContext();
        
        RimProxy p = new RimProxy(super.getRimUrl());
        p.openConnection();
        
        p.writeParam("command","retrieverequest");
        p.writeParam("objecttype","contentobj");
        
        p.writeParam("cak", super.getClientAccessKey());
        p.writeParam("registry", super.getRegistry());
        
        if ((this.mResponseType != null) && (this.mResponseType.length() > 0))
        {
            p.writeParam("responsetype", this.mResponseType);
        }
        
        p.writeParam("objidentifier", this.mContentObjectIdentifier);
        p.writeParam("minsidentifier", this.mMetadataInstanceIdentifier);
        
        rimResponse = p.writeResponseTo();
        
        p.close();
        
        return rimResponse;
    }
    
    /**
     * Ges the <CODE>String</CODE> response type handle of the response type to be used by RIM.
     * @return Returns the <CODE>String</CODE> response type handle which has been set as the response type to be used by RIM.
     */
    public String getResponseType()
    {
        return this.mResponseType;
    }
    
    /**
     *
     * @param iResponseType
     * @throws java.lang.NullPointerException
     */
    protected void setResponseType(String iResponseType)
    throws InvalidRequestException
    {
        if ((iResponseType == null) || (iResponseType.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iResponseType = iResponseType.trim();
        
        this.mResponseType = iResponseType;
    }
    
    /**
     * Ges the <CODE>String</CODE> content object identifier of the content object that is the target of the request.
     * @return Returns the <CODE>String</CODE> content object identifier which has been set as the target of the request.
     */
    public String getContentObjectIdentifier()
    {
        return this.mContentObjectIdentifier;
    }
    
    /**
     *
     * @param iContentObjectIdentifier
     * @throws java.lang.NullPointerException
     */
    protected void setContentObjectIdentifier(String iContentObjectIdentifier)
    throws InvalidRequestException
    {
        if ((iContentObjectIdentifier == null) || (iContentObjectIdentifier.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iContentObjectIdentifier = iContentObjectIdentifier.trim();
        
        this.mContentObjectIdentifier = iContentObjectIdentifier;
    }
    
    /**
     * Ges the <CODE>String</CODE> content metadata instance identifier of the metadata instance that is the target of the request.
     * @return Returns the <CODE>String</CODE> metadata instance identifier which has been set as the target of the request.
     */
    public String getMetadataInstanceIdentifier()
    {
        return this.mContentObjectIdentifier;
    }
    
    /**
     *
     * @param iMetadataInstanceIdentifier
     * @throws java.lang.NullPointerException
     */
    protected void setMetadataInstanceIdentifier(String iMetadataInstanceIdentifier)
    throws InvalidRequestException
    {
        if ((iMetadataInstanceIdentifier == null) || (iMetadataInstanceIdentifier.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iMetadataInstanceIdentifier = iMetadataInstanceIdentifier.trim();
        
        this.mMetadataInstanceIdentifier = iMetadataInstanceIdentifier;
    }
}
